#!/usr/bin/env node
import { Command } from 'commander';
import inquirer from 'inquirer';
import { checkPath, downloadTemp } from './utils.js';
import packageJson from '../package.json';
import fs, { readFileSync, writeFileSync } from 'node:fs';
function init() {
    const program = new Command(packageJson.name);

    program
        .version(packageJson.version)
        .arguments('<project-directory>')
        .description('create a new project')
        .action((project) => {
            // 命令行交互工具
            inquirer
                .prompt([
                    {
                        type: 'input',
                        name: 'projectName',
                        message: 'project name',
                        default: project,
                    }
                ])
                .then((answers) => {
                    const { projectName } = answers;
                    if (checkPath(projectName)) {
                        console.log('文件已存在');
                        return;
                    }

                    downloadTemp('master', projectName).then(() => {
                        const exitPackageJson = fs.existsSync(`${projectName}/package.json`);
                        if (exitPackageJson) {
                            const packageJsonPath = `${projectName}/package.json`;
                            const exitPackageJson = fs.existsSync(packageJsonPath);
                            if (exitPackageJson) {
                                const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
                                // 修改package.json
                                let packageJsonNew = {
                                    ...packageJson,
                                    name: projectName,
                                    description: `${projectName}应用`,
                                    homepage: '',
                                    bugs: {
                                        url: 'https://',
                                    },
                                    repository: {
                                        type: '',
                                        url: '',
                                    },
                                    author: '',
                                };

                                writeFileSync(`${projectName}/package.json`, JSON.stringify(packageJsonNew, '', '\t'));
                            }
                        }
                    });
                });
        })
        .allowUnknownOption()
        .parse(process.argv);
}

init();
